package it.polimi.travelDream.web.beans;


import java.util.ArrayList;
import java.util.List;

import it.polimi.it.TravelDream.ejb.PackageMgr;
import it.polimi.it.TravelDream.ejb.UserMgr;
import it.polimi.it.TravelDream.ejb.eaos.ExcursionDTO;
import it.polimi.it.TravelDream.ejb.eaos.FlightDTO;
import it.polimi.it.TravelDream.ejb.eaos.HotelDTO;
import it.polimi.it.TravelDream.ejb.eaos.PackageDTO;
import it.polimi.it.TravelDream.ejb.eaos.PersPackageMgr;
import it.polimi.it.TravelDream.ejb.eaos.PersonalizePackageDTO;
import it.polimi.it.TravelDream.ejb.eaos.SearchDTO;
import it.polimi.it.TravelDream.entities.BasicProdMgr;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class SearchBean {
	
	
	@EJB
	private PackageMgr packageMgr;
	
	@EJB
	private UserMgr userMgr;
	
	@EJB
	private PersPackageMgr persPackMgr;

	private SearchDTO search;
	
	private PackageDTO selectedPackage;
	
	private List<HotelDTO> hotelDetails;

	private List<FlightDTO> flightDetails;

	private List<ExcursionDTO> excursionDetails;

    private BasicProdMgr basicProdMgr;
    
    private String resultPersonalization;

//	private PersonalizePackageDTO packageSelected;
	
	

	

	
	
	public PackageDTO getSelectedPackage() {
		return selectedPackage;
	}

	public void setSelectedPackage(PackageDTO selectedPackage) {
		this.selectedPackage = selectedPackage;
	}

	private List<PackageDTO> packageList;
	
	private List<PackageDTO> filteredPackage;

	public List<PackageDTO> getFilteredPackage() {
		return filteredPackage;
	}

	public void setFilteredPackage(List<PackageDTO> filteredPackage) {
		this.filteredPackage = filteredPackage;
	}

	public SearchBean(){
		search = new SearchDTO();
	}
	
	public SearchDTO getSearch() {
		return search;
	}

	public void setSearch(SearchDTO search) {
		this.search = search;
	}
	public void printfabio(){
		packageMgr.printname();
	}
	
	
	
	public String getResultPersonalization() {
		return resultPersonalization;
	}

	public void setResultPersonalization(String resultPersonalization) {
		this.resultPersonalization = resultPersonalization;
	}

	@PostConstruct
	public void getKeyword() {
		packageList = new ArrayList<PackageDTO>();
	packageList = packageMgr.search(search.getKeySearch());
		
	}

	public List<PackageDTO> getPackageList() {
		return packageList;
	}

	public void setPackageList(List<PackageDTO> packageList) {
		this.packageList = packageList;
	}
	

public void purchase(){
		//l'utente che ha effettuato l'acquisto viene settato come idbuyer del package
		selectedPackage.setIdBuyer(userMgr.getUserDTO().getEmail());
		packageMgr.purchase(selectedPackage);
	}
	
//prende il pacchetto selezionato per la modifica e lo va ad aggiungere alla lista di pacchetti personalizzati dello user
public void personalizePackage(){
	resultPersonalization =persPackMgr.personalize(selectedPackage,userMgr.getUserDTO().getEmail());
}

public List<HotelDTO> getHotelDetails() {
	return hotelDetails;
}

public void setHotelDetails(List<HotelDTO> hotelDetails) {
	this.hotelDetails = hotelDetails;
}

public List<FlightDTO> getFlightDetails() {
	return flightDetails;
}

public void setFlightDetails(List<FlightDTO> flightDetails) {
	this.flightDetails = flightDetails;
}

public List<ExcursionDTO> getExcursionDetails() {
	return excursionDetails;
}

public void setExcursionDetails(List<ExcursionDTO> excursionDetails) {
	this.excursionDetails = excursionDetails;
}


/**
* Default constructor. 
*/


public void getHotelDetail(){
hotelDetails = new ArrayList<HotelDTO>();
//riceve il nome dell'hotel e restituisce i suoi dettagli nella lista
hotelDetails = basicProdMgr.getHotelDetails(selectedPackage.getIdBasicProduct2());


}

public void getFlightDetail(){
flightDetails = new ArrayList<FlightDTO>();
flightDetails = basicProdMgr.getFlightDetails(selectedPackage.getIdBasicProduct1());

}
public void getExcursionDetail(){
excursionDetails = new ArrayList<ExcursionDTO>();
excursionDetails =basicProdMgr.getExcursionDetails(selectedPackage.getIdBasicProduct3());
}





}
